import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public final class RedisUtil {

	private static final String ADDR = "localhost";
	private static final int PORT = 6379;
	private static final int MAX_ACTIVE = 1024;
	private static final int MAX_IDLE = 200;
	private static final int MAX_WAIT = 10000;
	private static final int TIMEOUT = 10000;
	private static final boolean TEST_ON_BORROW = true;
	private static JedisPool jedisPool;

	static {
		try {
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxActive(MAX_ACTIVE);
			config.setMaxIdle(MAX_IDLE);
			config.setMaxWait(MAX_WAIT);
			config.setTestOnBorrow(TEST_ON_BORROW);
			jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Jedis getJedis() {
		try {
			if(jedisPool != null) {
				Jedis res = jedisPool.getResource();
				return res;
			} else
				return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static void returnResource(final Jedis jedis) {
		if(jedis != null)
			jedisPool.returnResource(jedis);
	}
}
